package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.PageBean;
import com.itheima.mapper.ClassMapper;
import com.itheima.pojo.Classes;
import com.itheima.service.ClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;


@Service
public class ClassServiceImpl implements ClassService {

    @Autowired
    private ClassMapper classMapper;

    @Override
    public void insertOne(Classes classes) {
        classes.setCreateTime(LocalDateTime.now());
        classes.setUpdateTime(LocalDateTime.now());
        classMapper.insertOne(classes);

    }

    @Override
    public void deleteOne(Integer id) {
        classMapper.deleteOne(id);
    }

    @Override
    public Classes selectOne(Integer id) {
        Classes classes = classMapper.selectOne(id);

        return classes;
    }

    @Override
    public void updateOne(Classes classes) {
        classes.setUpdateTime(LocalDateTime.now());
        classMapper.updateOne(classes);

    }

    @Override
    public PageBean<Classes> selectPage(
            Integer page,
            Integer pageSize,
            String name,
            LocalDate begin,
            LocalDate end) {
        PageHelper.startPage(page,pageSize);

        Page<Classes> classesPage = classMapper.selectPage(name,begin,end);

        //定义新对象classesPageBean接收
        PageBean<Classes> classesPageBean = new PageBean<>();
        //接收数据
        classesPageBean.setTotal(classesPage.getTotal());
        classesPageBean.setRows(classesPage.getResult());

        return classesPageBean;
    }


}
